package com.achilles.fragment;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.SwitchPreference;
import android.widget.TimePicker;

import com.achilles.playtask.R;
import com.achilles.utils.AlarmUtils;
import com.achilles.utils.DateUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class SettingFragment extends PreferenceFragment implements TimePickerDialog.OnTimeSetListener, Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener {

    public static final int INIT_REMAIN_HOUR = 22;
    public static final int INIT_REMAIN_MINUS = 0;

    private SwitchPreference mSwitchPreferenceRemainTime;
    private Preference mRemainTimeSetting,mAuthor;
    private TimePickerDialog mTimePickerDialog;
    private int mRemainHour = INIT_REMAIN_HOUR;
    private int mRemainMinute = INIT_REMAIN_MINUS;


    public static Fragment getInstance() {
        SettingFragment settingFragment = new SettingFragment();
        return settingFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.setting_preferences);

        if (AlarmUtils.isAlarmEnable(getActivity())) {
            mRemainHour = AlarmUtils.getLocalAlarmHour(getActivity());
            mRemainMinute = AlarmUtils.getLocalAlarmMinute(getActivity());
        }

        String hour = DateUtils.getTwoDigitNumber(mRemainHour);
        String minute = DateUtils.getTwoDigitNumber(mRemainMinute);

        mTimePickerDialog = new TimePickerDialog(getActivity(), this, mRemainHour, mRemainMinute, true);
        mTimePickerDialog.setTitle(R.string.setting_remain_time_picker_title);

        mSwitchPreferenceRemainTime = (SwitchPreference) findPreference("setting_remain_time");
        mRemainTimeSetting = findPreference("setting_remain_time2");
        mRemainTimeSetting.setSummary(getString(R.string.setting_remain_setting_summary, hour, minute));

        mSwitchPreferenceRemainTime.setOnPreferenceChangeListener(this);
        mRemainTimeSetting.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                mTimePickerDialog.show();
                return false;
            }
        });

        mAuthor = findPreference("setting_author");
        mAuthor.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setMessage(R.string.setting_author_info);
                builder.create().show();
                return false;
            }
        });
    }

    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {
        mRemainHour = i;
        mRemainMinute = i1;

        String hour = DateUtils.getTwoDigitNumber(i);
        String minute = DateUtils.getTwoDigitNumber(i1);

        mRemainTimeSetting.setSummary(getString(R.string.setting_remain_setting_summary, hour, minute));
        AlarmUtils.setAlarm(getActivity(), mRemainHour, mRemainMinute);
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        return true;
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object o) {
        if (preference == mSwitchPreferenceRemainTime) {
            if ((Boolean) o) {
                AlarmUtils.setAlarm(getActivity(), mRemainHour, mRemainMinute);
            } else {
                AlarmUtils.cancelAlarm(getActivity());
            }
        }
        return true;
    }


}
